package cn.iocoder.yudao.module.biz.controller.admin.staffDept;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.controller.admin.staffDept.vo.*;
import cn.iocoder.yudao.module.biz.convert.staffDept.BizStaffDeptConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.staffDept.BizStaffDeptDO;
import cn.iocoder.yudao.module.biz.service.staffDept.BizStaffDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 员工部门")
@RestController
@RequestMapping("/biz/staff-dept")
@Validated
public class BizStaffDeptController {

    @Resource
    private BizStaffDeptService staffDeptService;


    @PostMapping("/create")
    @ApiOperation("创建员工部门")
    @PreAuthorize("@ss.hasPermission('biz:staff-dept:create')")
    public CommonResult<String> createStaffDept(@Valid @RequestBody BizStaffDeptCreateReqVO createReqVO) {
        return success(staffDeptService.createStaffDept(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新员工部门")
    @PreAuthorize("@ss.hasPermission('biz:staff-dept:update')")
    public CommonResult<Boolean> updateStaffDept(@Valid @RequestBody BizStaffDeptUpdateReqVO updateReqVO) {
        staffDeptService.updateStaffDept(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除员工部门")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:staff-dept:delete')")
    public CommonResult<Boolean> deleteStaffDept(@RequestParam("id") String id) {
        staffDeptService.deleteStaffDept(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得员工部门")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:staff-dept:query')")
    public CommonResult<BizStaffDeptRespVO> getStaffDept(@RequestParam("id") String id) {
        BizStaffDeptDO staffDept = staffDeptService.getStaffDept(id);
        return success(BizStaffDeptConvert.INSTANCE.convert(staffDept));
    }

    @GetMapping("/list")
    @ApiOperation("获得员工部门列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:staff-dept:query')")
    public CommonResult<List<BizStaffDeptRespVO>> getStaffDeptList(@RequestParam("ids") Collection<String> ids) {
        List<BizStaffDeptDO> list = staffDeptService.getStaffDeptList(ids);
        return success(BizStaffDeptConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得员工部门分页")
    @PreAuthorize("@ss.hasPermission('biz:staff-dept:query')")
    public CommonResult<PageResult<BizStaffDeptRespVO>> getStaffDeptPage(@Valid BizStaffDeptPageReqVO pageVO) {
        PageResult<BizStaffDeptDO> pageResult = staffDeptService.getStaffDeptPage(pageVO);
        return success(BizStaffDeptConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出员工部门 Excel")
    @PreAuthorize("@ss.hasPermission('biz:staff-dept:export')")
    @OperateLog(type = EXPORT)
    public void exportStaffDeptExcel(@Valid BizStaffDeptExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizStaffDeptDO> list = staffDeptService.getStaffDeptList(exportReqVO);
        // 导出 Excel
        List<BizStaffDeptExcelVO> datas = BizStaffDeptConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "员工部门.xls", "数据", BizStaffDeptExcelVO.class, datas);
    }

}
